package lg.minecraft.plugin.library.inventory;

import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public abstract class LGInventory {

    private static final Map<Player, LGInventory> inventoryMap = new HashMap<>();

    protected final int size;
    protected final String title;
    protected final Inventory inventory;

    protected boolean allowClick = false;
    /** 是否允许点击玩家背包(如: 设置为 ture 需 同时设置 allowClick 为 true) */
    protected boolean allowClickPlayerInventory = false;

    public LGInventory(int line, String title) {
        line = line < 0 || line > 6 ? 1 : line;
        this.size = line * 9;
        this.title = title;
        this.inventory = Bukkit.createInventory(null, size, title);
    }

    public LGInventory(int line) {
        this(line, "inventory");
    }

    public final void openInventory(Player player) {
        player.getOpenInventory().close();
        LGInventory.setInventory(player, this);
        player.openInventory(inventory);
    }

    public abstract void onInventoryOpen(CancelInventoryContext context);

    public abstract void onInventoryClick(ClickInventoryContext context);

    public abstract void onInventoryClose(InventoryContext context);

    public static LGInventory getInventory(Player player) {
        return inventoryMap.get(player);
    }

    public static void setInventory(Player player, LGInventory inventory) {
        inventoryMap.put(player, inventory);
    }

    public static void removeInventory(Player player) {
        inventoryMap.remove(player);
    }

    public static void removeInventory(Player player, LGInventory tInventory) {
        inventoryMap.remove(player, tInventory);
    }

    public static boolean hasInventory(Player player) {
        return inventoryMap.containsKey(player);
    }

}
